package com.qs.eggs.view.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.account.LoginActivity;
import com.qs.eggs.api.HttpApi;
import com.qs.eggs.base.fragment.BaseFragment;
import com.qs.eggs.bean.SimpleBackPage;
import com.qs.eggs.user.UserInfoActivity;
import com.qs.eggs.utils.UIHelper;
import com.qs.eggs.view.activity.AddressManagementActivity;
import com.qs.eggs.view.activity.CollectActivity;
import com.qs.eggs.view.activity.FootPrintActivity;
import com.qs.eggs.view.activity.OrderManagerActivity;
import com.qs.eggs.view.activity.RechargeRecordActivity;
import com.qs.eggs.view.prodetail.activity.ProDetailActivity;
import com.qs.eggs.view.recharge.activity.RechargeActivity;
import com.qs.eggs.widgets.custom.PullToRefreshView;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;

import okhttp3.Call;
import okhttp3.Response;

public class MineFragment extends BaseFragment implements OnClickListener,PullToRefreshView.OnHeaderRefreshListener, PullToRefreshView.OnFooterRefreshListener{
    private PullToRefreshView mPullToRefreshView;
    private TextView allOrder,custname;
    private SimpleDraweeView mAvator;
    private LinearLayout waitPay,mll2,mll3,mll4,rl1;

    private TextView mCollect,zuji,address,chongzhijilu,kefu,yue;
    private String xingming,imageUrl,balance;
    private RelativeLayout chongzhi;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;

    }
    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        mAvator=findView(R.id.profile_image);
        custname=findView(R.id.custname);
        rl1=findView(R.id.rl1);
        yue=findView(R.id.tv_chongzhi);
        chongzhi=findView(R.id.rl_chongzhi);
        allOrder=findView(R.id.tv_order_all);
        waitPay=findView(R.id.lly_waitpay);
        mCollect=findView(R.id.tv_collect);
        zuji=findView(R.id.tv_zuji);
        address=findView(R.id.tv_address);
        chongzhijilu=findView(R.id.tv_chongzhijilu);
        kefu=findView(R.id.tv_kefu);
        mPullToRefreshView =findView(R.id.main_pull_refresh_view);

        mll2=findView(R.id.ll2);
        mll3=findView(R.id.ll3);
        mll4=findView(R.id.ll4);

        mAvator.setOnClickListener(this);
        allOrder.setOnClickListener(this);
        waitPay.setOnClickListener(this);
        mll2.setOnClickListener(this);
        mll3.setOnClickListener(this);
        mll4.setOnClickListener(this);
        mCollect.setOnClickListener(this);
        zuji.setOnClickListener(this);
        address.setOnClickListener(this);
        chongzhijilu.setOnClickListener(this);
        kefu.setOnClickListener(this);
        rl1.setOnClickListener(this);
        chongzhi.setOnClickListener(this);
        mPullToRefreshView.setOnHeaderRefreshListener(this);
        mPullToRefreshView.setOnFooterRefreshListener(this);
    }
    @Override
    protected void initData() {
        super.initData();
         getInfo();

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl1:
                startActivity(new Intent(getContext(),UserInfoActivity.class));
                break;
            case R.id.tv_order_all:
                startActivity(new Intent(getContext(),OrderManagerActivity.class));
                break;
            case R.id.lly_waitpay:
                UIHelper.showSimpleBack(getContext(),
                        SimpleBackPage.ORDER_MANAGER);
                break;
            case R.id.ll2:
                UIHelper.showSimpleBack(getContext(),
                        SimpleBackPage.ORDER_MANAGER);
                break;
            case R.id.ll3:
                UIHelper.showSimpleBack(getContext(),
                        SimpleBackPage.ORDER_MANAGER);
                break;
            case R.id.ll4:
                UIHelper.showSimpleBack(getContext(),
                        SimpleBackPage.ORDER_MANAGER);
                break;
            case R.id.rl_chongzhi:
                startActivity(new Intent(getContext(),RechargeActivity.class));
                break;
            case R.id.tv_collect:
                startActivity(new Intent(getContext(), CollectActivity.class));
                break;
            case R.id.tv_zuji:
                startActivity(new Intent(getContext(), FootPrintActivity.class));
                break;
            case R.id.tv_chongzhijilu:
                startActivity(new Intent(getContext(), RechargeRecordActivity.class));
                break;
            case R.id.tv_address:
                Intent intent=new Intent(getActivity(),AddressManagementActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_kefu:
                Intent intent1 = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:13561753715"));
                intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent1);
                break;
        }
    }
    //获取个人信息
    private void getInfo() {
        HttpApi.cheakIsLogin(new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                if (s.equals("\"true\"")) {
                    OkGo.post(Constants.URL_BASE + "personal?action=getPersonalInfo")
                            .execute(new StringCallback() {
                                @Override
                                public void onSuccess(String s, Call call, Response response) {
                                    System.out.print("个人信息==" + s);
                                    try {
                                        JSONArray jsonArray = new JSONArray(s);
                                        for (int i = 0; i < jsonArray.length(); i++) {
                                            xingming = jsonArray.getJSONObject(i).getString("name");
                                            balance = jsonArray.getJSONObject(i).getString("balance");
                                            imageUrl = Constants.URL_BASE + jsonArray.getJSONObject(i).getString("folder") + "/"
                                                    + jsonArray.getJSONObject(i).getString("autoname");
                                            Picasso.with(getActivity()).load(imageUrl).placeholder(R.mipmap.icon_personimg).error(R.mipmap.icon_personimg).into(mAvator);
                                            custname.setText(xingming);
                                            yue.setText("账户余额: " + "¥" + balance);
                                        }

                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                } else {
                    LoginActivity.show(getContext());
                }

            }
        });
    }

    @Override
    public void onFooterRefresh(PullToRefreshView view) {
        mPullToRefreshView.postDelayed(new Runnable() {

            @Override
            public void run() {
                handler.sendEmptyMessage(1);
                getInfo();
                mPullToRefreshView.onFooterRefreshComplete();
            }
        }, 1000);
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        mPullToRefreshView.postDelayed(new Runnable() {

            @Override
            public void run() {
                handler.sendEmptyMessage(1);
                getInfo();
                mPullToRefreshView.onHeaderRefreshComplete();
            }
        }, 1000);

    }
	/*-----------刷新接口方法实现-------	*/

    Handler handler = new Handler() {
        public void handleMessage(Message message) {
            super.handleMessage(message);
            mPullToRefreshView.onFooterRefreshComplete();

        }

        ;
    };

}
